/**
 * 
 */
package jvs.vfs.opt.contact;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import jvs.vfs.IFile;
import jvs.vfs.opt.ItemInfo;
import jvs.vfs.opt.ItemListFileImpl;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class PhoneContactFileImpl extends ItemListFileImpl {
	private static final String[] LIST = { "/all", "/favorite", "/most" , "/recent",
			};
	private static ItemInfo[][] lists = new ItemInfo[LIST.length][];

	private static final int LIST_ALL = 0;

	private static final int LIST_FAVE = 1;

	private static final int LIST_MOST = 2;

	private static final int LIST_RECENT = 3; // last time called

	private static final String LOG_TAG = "PhoneContactFileImpl";

	private static final String SP = " ";

	synchronized private static void loadAll(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadPhoneContact(ctx, list, idx, ContactAdapter.SORT_BY_DISPLAY_NAME);
	}

	synchronized private static void loadFave(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadPhoneFavorite(ctx, list, idx, ContactAdapter.SORT_BY_DISPLAY_NAME);
	}

	synchronized private static void loadMost(Context ctx,
			List<ContactInfo> list, int idx) {
		
		ContactUtils.loadPhoneContact(ctx, list, idx, ContactAdapter.SORT_BY_TIMES_CONTACTED);
	}

	synchronized private static void loadRecent(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadPhoneContact(ctx, list, idx, ContactAdapter.SORT_BY_LAST_TIME_CONTACTED);
	}

	/**
	 * @param uri
	 */
	public PhoneContactFileImpl(URI uri) {
		super(uri);
		//
		if (DEBUG) {
			Log.d(LOG_TAG, "PhoneContactFileImpl " + uri);
		}
	}

	protected PhoneContactFileImpl(URI pu, ItemInfo ai) {
		super(pu, ai);
	}

	private void appendLabel(StringBuilder sb, ContactInfo item) {
		String lab = item.phoneLabel;
		if (TextUtils.isEmpty(lab)) {
			switch (item.phoneType) {
			case ContactInfo.TYPE_HOME:
				sb.append(SP);
				sb.append("Home");
				break;
			case ContactInfo.TYPE_WORK:
				sb.append(SP);
				sb.append("Work");
				break;
			case ContactInfo.TYPE_MOBILE:
				sb.append(SP);
				sb.append("Mobile");
				break;
			default:
			}
		} else {
			sb.append(SP);
			sb.append(lab);
		}
	}

	@Override
	public boolean execute() {
		try {
			final ContactInfo item = (ContactInfo) info;
			if (item == null) {
				return false;
			}
			Context ctx = (Context) fs.getContext();
			Intent i = ContactUtils.createCallIntent(item.phone);
			String ti = makeName(item);
			Intent ci = Intent.createChooser(i, ti);
			ci.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startActivity(ci);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public String getDisplayName() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return makeNamePhone(item);
		}
		return super.getDisplayName();
	}

	@Override
	protected String[] getFolders() {
		return LIST;
	}

	@Override
	public Object getIcon() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			Object obj = item.icon;
			if (obj == null) {
				Context ctx = (Context) fs.getContext();
				Resources res = ctx.getResources();
				return res.getDrawable(android.R.drawable.sym_action_call);
			}
		}
		return super.getIcon();
	}

	@Override
	public long getLastModified() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return item.lastTime;
		}
		return super.getLastModified();
	}

	@Override
	public long getLength() {
		return 0;
	}

	@Override
	public String getPhoneticName() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			String pn = item.phoneticName;
			if (TextUtils.isEmpty(pn)) {
				return makeName(item);
			} else {
				return pn;
			}
		}
		return super.getPhoneticName();
	}

	@Override
	public String getSortKey() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return item.sortkey;
		}
		return super.getSortKey();
	}

	@Override
	protected ItemInfo[] load(Context ctx, int idx) {
		ArrayList<ContactInfo> list = new ArrayList<ContactInfo>();
		switch (idx) {
		case LIST_ALL:
			loadAll(ctx, list, idx);
			break;
		case LIST_FAVE:
			loadFave(ctx, list, idx);
			break;
		case LIST_RECENT:
			loadRecent(ctx, list, idx);
			break;
		case LIST_MOST:
			loadMost(ctx, list, idx);
			break;
		}
		if (DEBUG) {
			Log.d(LOG_TAG, "loaded: [" + idx + "] " + list);
		}
		return list.toArray(new ItemInfo[0]);
	}

	private String makeName(ContactInfo item) {
		StringBuilder sb = new StringBuilder(item.name);
		appendLabel(sb, item);
		return sb.toString();
	}

	private String makeNamePhone(ContactInfo item) {
		StringBuilder sb = new StringBuilder(item.name);
		appendLabel(sb, item);
		sb.append(SP);
		sb.append(item.phone);
		return sb.toString();
	}

	@Override
	protected IFile newInstance(URI pu, ItemInfo ai) {
		return new PhoneContactFileImpl(pu, ai);
	}

	@Override
	protected ItemInfo[][] lists() {
		return lists;
	}
}
